package dicky.fortunetelling;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;


import dicky.fortunetelling.logic.BaZi;
import dicky.fortunetelling.logic.DataArray;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class fivemarry extends Activity {
	private Button btn1;
	private EditText date1,time1,date2,time2;
	private TextView text1,text2;
	private Calendar cal1 = Calendar.getInstance();
	private Calendar cal2 = Calendar.getInstance();
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private SimpleDateFormat tf = new SimpleDateFormat("HH:mm");
	
	private String tag=fivemarry.class.getSimpleName();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fivemarry);
        
        btn1=(Button)findViewById(R.id.button1);
        btn1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				fivemarrysearch();
			}
		});
        
        date1=(EditText)findViewById(R.id.date1);
        date1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				DatePickerDialog dg = new DatePickerDialog(
						fivemarry.this, datelistener1, cal1
								.get(Calendar.YEAR), cal1.get(Calendar.MONTH),
						cal1.get(Calendar.DAY_OF_MONTH));
				dg.setTitle("选择日期");
				dg.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});

        time1=(EditText)findViewById(R.id.time1);
        time1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				TimePickerDialog dg = new TimePickerDialog(fivemarry.this,
						timelistener1, cal1.get(Calendar.HOUR_OF_DAY), cal1
								.get(Calendar.MINUTE), true);
				dg.setTitle("选择时间");
				dg.setButton(TimePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});
        

        date2=(EditText)findViewById(R.id.date2);
        date2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				DatePickerDialog dg = new DatePickerDialog(
						fivemarry.this, datelistener2, cal2
								.get(Calendar.YEAR), cal2.get(Calendar.MONTH),
						cal2.get(Calendar.DAY_OF_MONTH));
				dg.setTitle("选择日期");
				dg.setButton(DatePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});

        time2=(EditText)findViewById(R.id.time2);
        time2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				TimePickerDialog dg = new TimePickerDialog(fivemarry.this,
						timelistener2, cal2.get(Calendar.HOUR_OF_DAY), cal2
								.get(Calendar.MINUTE), true);
				dg.setTitle("选择时间");
				dg.setButton(TimePickerDialog.BUTTON_POSITIVE, "选择", dg);
				dg.show();
			}
		});
        
        text1=(TextView)findViewById(R.id.text1);
        text2=(TextView)findViewById(R.id.text2);
    }

	private DatePickerDialog.OnDateSetListener datelistener1 = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal1.set(Calendar.YEAR, year);
			cal1.set(Calendar.MONTH, monthOfYear);
			cal1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			date1.setText(df.format(cal1.getTime()));
		}
	};


	private DatePickerDialog.OnDateSetListener datelistener2 = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			cal2.set(Calendar.YEAR, year);
			cal2.set(Calendar.MONTH, monthOfYear);
			cal2.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			
			date2.setText(df.format(cal2.getTime()));
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timelistener1 = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			cal1.set(Calendar.HOUR_OF_DAY, hourOfDay);
			cal1.set(Calendar.MINUTE, minute);
			
			time1.setText(tf.format(cal1.getTime()));
		}
	};
	
	private TimePickerDialog.OnTimeSetListener timelistener2 = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			cal2.set(Calendar.HOUR_OF_DAY, hourOfDay);
			cal2.set(Calendar.MINUTE, minute);
			
			time2.setText(tf.format(cal2.getTime()));
		}
	};
		
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			if (getCurrentFocus() != null
					&& getCurrentFocus().getWindowToken() != null) {
				manager.hideSoftInputFromWindow(getCurrentFocus()
						.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
		return super.onTouchEvent(event);

	}
	
	private void fivemarrysearch() {
		try {
			String d1 = date1.getText().toString();
			String t1 = time1.getText().toString();
			String d2 = date2.getText().toString();
			String t2 = time2.getText().toString();
			if ("".equals(d1) || "".equals(t1)) {
				Toast.makeText(fivemarry.this, "请录入男方出生日期及时间", Toast.LENGTH_LONG).show();
				return;
			}
			
			if ("".equals(d2) || "".equals(t2)) {
				Toast.makeText(fivemarry.this, "请录入女方出生日期及时间", Toast.LENGTH_LONG).show();
				return;
			}
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(df.parse(d1));
			int h1 = Integer.valueOf(t1.substring(0, 2));
			int hour1[] = this.changehour(h1);
			cal1.add(cal1.DATE, hour1[1]);
			
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(df.parse(d2));
			int h2 = Integer.valueOf(t2.substring(0, 2));
			int hour2[] = this.changehour(h2);
			cal2.add(cal2.DATE, hour2[1]);
			
			BaZi lunar1 = new BaZi(cal1);
			BaZi lunar2 = new BaZi(cal2);
			String year1=lunar1.getYearGanZhi(hour1[0]).substring(0,2);
			String year2=lunar2.getYearGanZhi(hour2[0]).substring(0,2);
			HashMap map1=DataArray.GetYearFate(year1);
			HashMap map2=DataArray.GetYearFate(year2);
			HashMap map=DataArray.FiveMarryMap();
			String five1=map1.get("five")+"";
			String five2=map2.get("five")+"";
			String key="男"+five1+"女"+five2;
			text1.setText(key);
			text2.setText(map.get(key)+"");
		} catch (Exception e) {
			Log.e(tag, e+"");
			Toast.makeText(fivemarry.this, "五行婚配查询失败", Toast.LENGTH_LONG).show();
		}
	}

	private int[] changehour(int h){
		int hour[] = new int[]{1,0};
		if (h >= 1) {
			hour[0] = 2;
		}
		if (h >= 3) {
			hour[0] = 3;
		}
		if (h >= 5) {
			hour[0] = 4;
		}
		if (h >= 7) {
			hour[0] = 5;
		}
		if (h >= 9) {
			hour[0] = 6;
		}
		if (h >= 11) {
			hour[0] = 7;
		}
		if (h >= 13) {
			hour[0] = 8;
		}
		if (h >= 15) {
			hour[0] = 9;
		}
		if (h >= 17) {
			hour[0] = 10;
		}
		if (h >= 19) {
			hour[0] = 11;
		}
		if (h >= 21) {
			hour[0] = 12;
		}
		if (h >= 23) {
			hour[0] = 1;
			hour[1] = 1;
		}
		return hour;
	}
}
